<template>
  <div>
    <section class="header clearfix">
      <span>区域缴纳</span>
      <span class="iconfont iconadd1" @click="add"> 添加</span>
    </section>
    <div class="search-box" @keyup.enter="searchFun">
      <i class="iconfont iconsearch1"></i>
      <el-input
        placeholder="输入区域缴纳名称进行搜索"
        class="search"
        v-model.trim="searchContent"
      ></el-input>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        v-loading="loading"
        style="width: 100%"
      >
        <el-table-column
          prop="areaPaymentId"
          label="区域缴纳项目ID"
        ></el-table-column>
        <el-table-column
          prop="paymentName"
          label="缴纳项目名称"
        ></el-table-column>
        <el-table-column
          prop="areaCode"
          label="缴纳城市"
          :formatter="areaCodeFilter"
        ></el-table-column>
        <el-table-column
          prop="paymentType"
          label="缴纳方式"
          :formatter="typeFilter"
        ></el-table-column>
        <el-table-column
          prop="defaultVal"
          label="默认值"
          :formatter="defaultValFormatter"
        ></el-table-column>
        <el-table-column
          prop="baseVal"
          label="基数"
          :formatter="defaultValFormatter"
        ></el-table-column>
        <el-table-column
          prop="companyScale"
          label="公司比例"
          :formatter="valueFormatter"
        ></el-table-column>
        <el-table-column
          prop="personScale"
          label="个人比例"
          :formatter="valueFormatter"
        ></el-table-column>
        <el-table-column label="详情" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="getAreaPaymentDetail(scope.row.areaPaymentId)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="remove(scope.row.areaPaymentId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!--新增对话框-->
    <el-dialog
      title="新增区域缴纳项目"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      center
    >
      <el-form :model="form">
        <el-form-item label="缴纳项目" :label-width="formLabelWidth" required>
          <el-select v-model="form.paymentId" filterable placeholder="请选择">
            <el-option
              v-for="item in paymentOptions"
              :key="item.paymentId"
              :label="item.paymentName"
              :value="item.paymentId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区域代码" :label-width="formLabelWidth" required>
          <el-select v-model="form.areaCode" filterable placeholder="请选择">
            <el-option
              v-for="item in areaCodeOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴纳方式" :label-width="formLabelWidth" required>
          <el-select
            v-model="form.paymentType"
            @change="typeChange"
            placeholder="请选择"
          >
            <el-option
              v-for="item in paymentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 0"
          label="默认值"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.defaultVal"
            clearable
            placeholder="请输入默认值"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 1"
          label="公司比例"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.companyScale"
            clearable
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入公司比例"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 1"
          label="个人比例"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.personScale"
            clearable
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入个人比例"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否可补缴" :label-width="formLabelWidth" required>
          <el-select v-model="form.isSupplementary" placeholder="请选择">
            <el-option
              v-for="item in isSupplementaryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跨年补缴" :label-width="formLabelWidth" required>
          <el-select v-model="form.isYear" placeholder="请选择">
            <el-option
              v-for="item in isSupplementaryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="基数"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseVal"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数最小值"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseValMin"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数最大值"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseValMax"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="申报日" :label-width="formLabelWidth" required>
          <el-select v-model="form.reportDate" filterable placeholder="请选择">
            <el-option
              v-for="item in dateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            addLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="addLoading" @click="confirmAdd()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!--修改对话框-->
    <el-dialog
      title="编辑区域缴纳项目"
      :visible.sync="dialogEdit"
      :close-on-click-modal="false"
      :show-close="false"
      center
    >
      <el-form :model="form">
        <el-form-item label="缴纳项目" :label-width="formLabelWidth">
          <el-select v-model="form.paymentId" disabled placeholder="请选择">
            <el-option
              v-for="item in paymentOptions"
              :key="item.paymentId"
              :label="item.paymentName"
              :value="item.paymentId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区域代码" :label-width="formLabelWidth" required>
          <el-select v-model="form.areaCode" filterable placeholder="请选择">
            <el-option
              v-for="item in areaCodeOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴纳方式" :label-width="formLabelWidth" required>
          <el-select
            v-model="form.paymentType"
            @change="typeChange"
            placeholder="请选择"
          >
            <el-option
              v-for="item in paymentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.paymentType == 0"
          label="默认值"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.defaultVal"
            clearable
            placeholder="请输入默认值"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="公司比例"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.companyScale"
            clearable
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入公司比例"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="个人比例"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.personScale"
            clearable
            suffix-icon="iconfont iconbaifenbi"
            placeholder="请输入个人比例"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否可补缴" :label-width="formLabelWidth" required>
          <el-select v-model="form.isSupplementary" placeholder="请选择">
            <el-option
              v-for="item in isSupplementaryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跨年补缴" :label-width="formLabelWidth" required>
          <el-select v-model="form.isYear" placeholder="请选择">
            <el-option
              v-for="item in isSupplementaryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="基数"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseVal"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数最小值"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseValMin"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="基数最大值"
          v-if="form.paymentType == 1"
          :label-width="formLabelWidth"
          required
        >
          <el-input
            v-model="form.baseValMax"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="申报日" :label-width="formLabelWidth" required>
          <el-select v-model="form.reportDate" filterable placeholder="请选择">
            <el-option
              v-for="item in dateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogEdit = false;
            editLoading = false;
          "
          >取 消</el-button
        >
        <el-button
          type="primary"
          :loading="editLoading"
          @click="confirmUpdate()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template> 

<script>
import base from "@/assets/js/base";
import { requestApiPost } from "../api/api";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
export default {
  name: "payManagement",
  data() {
    return {
      loading: false,
      addLoading: false,
      editLoading:false,
      formLabelWidth: "160px",
      isNumber: 0,
      searchContent: "",
      tableData: [],
      current: 1,
      pageSize: 6,
      total: null,
      form: {},
      dialogFormVisible: false,
      dialogEdit: false,
      paymentOptions: [],
      areaCodeOptions: [],
      paymentTypeOptions: [
        {
          value: "0",
          label: "值",
        },
        {
          value: "1",
          label: "比例",
        },
      ],
      isSupplementaryOptions: [
        {
          value: 1,
          label: "可以",
        },
        {
          value: 0,
          label: "不可以",
        },
      ],
      dateOptions: [
        {
          label: "1号",
          value: "1",
        },
        {
          label: "2号",
          value: "2",
        },
        {
          label: "3号",
          value: "3",
        },
        {
          label: "4号",
          value: "4",
        },
        {
          label: "5号",
          value: "5",
        },
        {
          label: "6号",
          value: "6",
        },
        {
          label: "7号",
          value: "7",
        },
        {
          label: "8号",
          value: "8",
        },
        {
          label: "9号",
          value: "9",
        },
        {
          label: "10号",
          value: "10",
        },
        {
          label: "11号",
          value: "11",
        },
        {
          label: "12号",
          value: "12",
        },
        {
          label: "13号",
          value: "13",
        },
        {
          label: "14号",
          value: "14",
        },
        {
          label: "15号",
          value: "15",
        },
        {
          label: "16号",
          value: "16",
        },
        {
          label: "17号",
          value: "17",
        },
        {
          label: "18号",
          value: "18",
        },
        {
          label: "19号",
          value: "19",
        },
        {
          label: "20号",
          value: "20",
        },
        {
          label: "21号",
          value: "21",
        },
        {
          label: "22号",
          value: "22",
        },
        {
          label: "23号",
          value: "23",
        },
        {
          label: "24号",
          value: "24",
        },
        {
          label: "25号",
          value: "25",
        },
        { label: "26号", value: "26" },
        {
          label: "27号",
          value: "27",
        },
        {
          label: "28号",
          value: "28",
        },
        {
          label: "29号",
          value: "29",
        },
        {
          label: "30号",
          value: "30",
        },
        {
          label: "31号",
          value: "31",
        },
      ],
    };
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.current = e;
      this.getData();
    },
    typeChange(e) {
      if (e == 0) {
        this.$set(this.form, "companyScale", "");
        this.$set(this.form, "personScale", "");
        this.$set(this.form, "baseVal", "");
        this.$set(this.form, "baseValMin", "");
        this.$set(this.form, "baseValMax", "");
      } else {
        this.$set(this.form, "defaultVal", "");
      }
    },
    //获取区域缴纳项目详情
    getAreaPaymentDetail(areaPaymentId) {
      var reqData = {
        areaPaymentId: areaPaymentId,
      };
      let reqReport = reqRep({ traCode: "_GET_AREA_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.form = resReport.data;
          //比例
          this.dialogEdit = true;
        }
      });
    },
    //确认更新
    confirmUpdate() {
      if (!this.form.paymentId || !this.form.areaCode) {
        this.$message.warning("信息不完整");
        return;
      }
      let data = JSON.parse(JSON.stringify(this.form));
      if (this.form.paymentType == 0) {
        //值
        if (!this.form.defaultVal) {
          this.$message.warning("请输入默认值");
          return;
        }
      } else {
        //比例
        if (
          !this.form.companyScale ||
          !this.form.personScale ||
          !this.form.baseValMin ||
          !this.form.baseValMax ||
          !this.form.baseVal
        ) {
          this.$message.warning("信息不完整");
          return;
        }
      }
      this.editLoading = true;
      let reqReport = reqRep({ traCode: "_EDIT_AREA_PAYMENT", data: data });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.editLoading = false;
        } else {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.dialogEdit = false;
          this.editLoading = false;
          this.getData();
        }
      });
    },
    //删除
    remove(id) {
      this.$confirm("确定删除该区域缴纳项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.removePayment(id);
        })
        .catch(() => {});
    },
    removePayment(id) {
      var reqData = {
        areaPaymentId: id,
      };
      let reqReport = reqRep({ traCode: "_DEL_AREA_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        }
      });
    },
    //新增
    add() {
      this.$set(this.form, "paymentId", "");
      this.$set(this.form, "areaCode", "");
      this.$set(this.form, "paymentType", "1");
      this.$set(this.form, "defaultVal", "");
      this.$set(this.form, "companyScale", "");
      this.$set(this.form, "personScale", "");
      this.$set(this.form, "isSupplementary", 1);
      this.$set(this.form, "isYear", 1);
      this.$set(this.form, "baseValMin", "");
      this.$set(this.form, "baseValMax", "");
      this.$set(this.form, "baseVal", "");
      this.$set(this.form, "reportDate", "");
      this.dialogFormVisible = true;
    },
    confirmAdd() {
      if (
        !this.form.paymentId ||
        !this.form.areaCode ||
        !this.form.reportDate
      ) {
        this.$message.warning("信息不完整");
        return;
      }

      let data = JSON.parse(JSON.stringify(this.form));

      if (this.form.paymentType == 0) {
        //值
        if (!this.form.defaultVal) {
          this.$message.warning("请输入默认值");
          return;
        }
      } else {
        //比例
        if (
          !this.form.companyScale ||
          !this.form.personScale ||
          !this.form.baseValMin ||
          !this.form.baseValMax ||
          !this.form.baseVal
        ) {
          this.$message.warning("信息不完整");
          return;
        } 
      }
      this.addLoading = true;
      let reqReport = reqRep({ traCode: "_ADD_AREA_PAYMENT", data: data });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.addLoading = false;
        } else {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.addLoading = false;
          this.getData();
        }
      });
    },
    //获取缴纳项目
    getPayment() {
      var reqData = {
        paymentName: "",
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.paymentOptions = resReport.data.records;
        }
      });
    },
    //获取区域列表
    getAreaCode() {
      var reqData = {
        grade: 1,
        isStart: 0,
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.areaCodeOptions = resReport.data.records;
          this.getData();
        }
      });
    },
    searchFun() {
      this.current = 1;
      this.tableData = [];
      this.getData();
    },
    //获取列表
    getData() {
      this.loading = true;
      var reqData = {
        paymentName: this.searchContent,
        current: this.current,
        pageSize: this.pageSize,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.tableData = resReport.data.records;
          this.total = parseInt(resReport.data.total);
        }
      });
    },
    areaCodeFilter(a, b, c) {
      for (let index = 0; index < this.areaCodeOptions.length; index++) {
        if (this.areaCodeOptions[index].areaCode == c) {
          return this.areaCodeOptions[index].areaName;
        }
      }
    },
    typeFilter(a, b, c) {
      if (c == 1) {
        return "比例";
      } else {
        return "值";
      }
    },
    defaultValFormatter(a, b, c) {
      if (c >= 0) {
        return c;
      } else {
        return "-";
      }
    },
    valueFormatter(a, b, c) {
      if (c > 0) {
        return c + "%";
      } else {
        return "-";
      }
    },
  },
  created() {
    this.getAreaCode();
    this.getPayment();
  },
  mounted() {},
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
}
.header .org {
  float: right;
  cursor: pointer;
}
.org:hover {
  color: #13cb6e;
}
.header i {
  font-size: 20px;
  margin-right: 5px;
}
.opt-user {
  padding: 20px 15px;
}
.opt-user > section {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.opt-user i {
  font-size: 20px;
  margin-right: 5px;
  vertical-align: middle;
}
.iconadd1 {
  float: right;
  font-size: 14px;
  color: #13cb6e;
  cursor: pointer;
}
.search-box {
  height: 50px;
  line-height: 50px;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
.search-box i {
  margin-left: 15px;
  vertical-align: middle;
}
.search-box .search {
  display: inline-block;
  width: calc(100% - 36px);
  box-sizing: border-box;
}
.search-box .option {
  display: inline-block;
  margin-left: 15px;
}
.search-box .el-select {
  width: 150px;
}
.search-box .el-select input {
  min-width: 100px;
  background-color: #eee;
}
.search-box .batchChange {
  display: inline-block;
  color: #999;
  font-size: 14px;
  cursor: pointer;
}
i {
  font-size: 16px;
  color: #999;
}
</style>
